﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.ProductLock
{
    public partial class ProductUnLockForm : DevExpress.XtraEditors.XtraForm
    {
        public ProductUnLockForm()
        {
            InitializeComponent();
        }
        int PageIndex = 1; // 当前页码
        int PageSize = 10; // 每页显示的记录数
        int PageCount = 0; // 总页数
        int Count = 0; // 总记录数 

        public async void ReInit()
        {
            List<ProductUnLockOutput> productUnLocks = await GetProductUnLock();
            gridControl1.DataSource = productUnLocks;

            var gridView = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (gridView != null)
            {
                gridView.Columns.Clear();

                // 添加多选框列
                var checkEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
                gridControl1.RepositoryItems.Add(checkEdit);
                var colSelect = gridView.Columns.AddVisible("IsSelected", "选择");
                colSelect.ColumnEdit = checkEdit;
                colSelect.VisibleIndex = 0; // 设置为第一列
                colSelect.Width = 40;
                //设置多选框默认不选中
                checkEdit.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;

                // 其他数据列
                gridView.Columns.AddVisible("PRODUCTLOCKTYPE", "类型");
                gridView.Columns.AddVisible("PRODUCTLOCKCODE", "编码");
                gridView.Columns.AddVisible("PRODUCTLOCKPRODUCTION", "工序");
                gridView.Columns.AddVisible("PRODUCTLOCKBEGINTIME", "锁定开始日期");
                gridView.Columns.AddVisible("PRODUCTLOCKENDTIME", "锁定结束日期");
                gridView.Columns.AddVisible("LOCKINGREASON", "锁定原因");
                gridView.Columns.AddVisible("CREATEUSERNAME", "锁定人");
                gridView.Columns.AddVisible("CREATETIME", "锁定时间");
            }
        }

        private void ProductUnLockForm_Load(object sender, EventArgs e)
        {
            ReInit();
            // 初始化分页控件
            lookUpEdit1.Properties.DataSource = new List<int> { 3, 5, 7, 10 };
            lookUpEdit1.EditValue = PageSize; // 设置默认值为10
            lookUpEdit1.Properties.NullText = "请选择每页显示的记录数"; // 下拉框启动时默认显示
            lookUpEdit1.EditValueChanged += lookUpEdit1_EditValueChanged; // 绑定事件处理程序
        }

        private async Task<List<ProductUnLockOutput>> GetProductUnLock()
        {
            using (var client = new HttpClient())
            {
                var response = await client.GetStringAsync($"http://localhost:5238/api/ProductLock/GetProductUnLock?PageIndex={PageIndex}&PageSize={PageSize}");
                var result = JsonConvert.DeserializeObject<ApiPaging<ProductUnLockOutput>>(response);
                Count = result.TotalCount;
                PageCount = (int)Math.Ceiling((Count * 1.0) / PageSize);
                return result.PageData;
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            if (PageIndex <= 1)
            {
                MessageBox.Show("已经是第一页了！");
                return;
            }
            PageIndex -= 1;
            ReInit();
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            if (PageIndex >= PageCount)
            {
                MessageBox.Show("已经是最后一页了！");
                return;
            }
            PageIndex += 1;
            ReInit();
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            PageSize = (int)lookUpEdit1.EditValue;
            ReInit();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            // 获取数据源
            var list = gridControl1.DataSource as List<ProductUnLockOutput>;
            if (list == null) return;

            var selected = list.Where(x => x.IsSelected).ToList();
            if (selected.Count == 0)
            {
                MessageBox.Show("请至少勾选一条记录！");
                return;
            }

            string sb = "";
            foreach (var row in selected)
            {
                if(sb!="")
                {
                    sb+=","; // 添加逗号分隔符
                }
                sb += row.PRODUCTLOCKCODE; // 假设PRODUCTLOCKCODE是唯一标识符
            }
            using (var client = new HttpClient())
            {
                var response = await client.DeleteAsync($"http://localhost:5238/api/ProductLock/DeleteProductUpLock?code={sb}");
                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("解锁成功!");
                    ReInit(); // 重新加载数据
                }
                else
                {
                    MessageBox.Show("解锁失败: " + response.ReasonPhrase);
                }
            }
        }
    }
}